package com.pubinfo.passbook.common.service.ruler.impl;

import com.pubinfo.passbook.common.model.bo.witeoff.CityInfo;
import com.pubinfo.passbook.common.model.constant.StaffStatusEnum;
import com.pubinfo.passbook.common.model.dubbo.ESBServiceData;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.service.aviator.AviatorScriptService;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.common.service.ruler.RuleTaizhouService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RuleTaizhouServiceImpl implements RuleTaizhouService {
  @Autowired
  DubboService dubboService;
  
  @Autowired
  AviatorScriptService scriptService;
  
  @Override
  public BigDecimal linHai(CityInfo info, Map<String, Object> paramEnv, List<PayInformation> payInformations, BigDecimal individualAccountCurr) {
    BigDecimal sum = new BigDecimal(0);
    
    String idNo = payInformations.get(0).getPayerPartyCode(); //报账人的身份证号
    ESBServiceData.CertDetail certDetail = dubboService.getPersonsDetailByIdNo(idNo);
    String statusName = certDetail.getStaffStatus();
    //工龄
    Integer workTime = Integer.valueOf(certDetail.getWorkTime());
    Integer status = StaffStatusEnum.getEnumByStatus(statusName).getCode();
    StaffStatusEnum statusEnum = StaffStatusEnum.getEnumByStatus(statusName);
    BigDecimal ownPayAmounts = BigDecimal.ZERO; //现金（支付宝等）
    BigDecimal individualAccountPrevPayments = BigDecimal.ZERO;//历年账户支付
    BigDecimal selfpaymentCosts = BigDecimal.ZERO;//自费
    BigDecimal selfFaceAmounts = BigDecimal.ZERO;//票面自负金额
    for (PayInformation payInformation : payInformations) {
      BigDecimal ownPayAmount = payInformation.getOwnPayAmount();
      if (ownPayAmount.equals(new BigDecimal(-0.0000999))) {
        paramEnv.put("isNeedAudit", true);
        ownPayAmount = BigDecimal.ZERO;
      }
      BigDecimal individualAccountPrevPayment = payInformation.getIndividualAccountPrevPayment();
      if (individualAccountPrevPayment.equals(new BigDecimal(-0.0000999))) {
        paramEnv.put("isNeedAudit", true);
        individualAccountPrevPayment = BigDecimal.ZERO;
      }
      BigDecimal selfpaymentCost = payInformation.getSelfpaymentCost();
      if (selfpaymentCost.equals(new BigDecimal(-0.0000999))) {
        paramEnv.put("isNeedAudit", true);
        selfpaymentCost = BigDecimal.ZERO;
      }
      Map<String, Object> env = new HashMap<>();
      env.put("ownPayAmount", ownPayAmount);
      env.put("individualAccountPrevPayment", individualAccountPrevPayment);
      env.put("selfpaymentCost", selfpaymentCost);
      // 注：票面自负金额=现金（支付宝等）+历年账户支付-自费
      BigDecimal selfFaceAmount = scriptService.execute("ownPayAmount+individualAccountPrevPayment-selfpaymentCost", env);
      if (selfFaceAmount.compareTo(BigDecimal.ZERO) != -1) {
        selfFaceAmounts = selfFaceAmount.add(selfFaceAmount);
      }
    }
  
    // 票面自负金额 = 当前票据票面自负金额+历史报销金额
    selfFaceAmounts = selfFaceAmounts.add(individualAccountCurr);
    BigDecimal scale = BigDecimal.ZERO;
    BigDecimal opcQuota = BigDecimal.ZERO; //门诊总控额度
    switch (statusEnum) {
      case ON_THE_JOB:
        if (workTime <= 10) {
          scale = new BigDecimal(0.7);
        } else if (workTime >= 11 && workTime <= 20) {
          scale = new BigDecimal(0.8);
        } else if (workTime >= 21 && workTime <= 30) {
          scale = new BigDecimal(0.85);
        } else if (workTime > 30) {
          
          scale = new BigDecimal(0.9);
        }
        opcQuota = new BigDecimal(3000);
        //在职
        break;
      case DEPARTURE:
        //退休
        scale = new BigDecimal(0.9);
        opcQuota = new BigDecimal(4000);
        break;
      case EARLY_RETIREMENT:
        //内退
        if (workTime >= 30) {
          scale = new BigDecimal(0.9);
        }
        opcQuota = new BigDecimal(4000);
        break;
      case RETIRE:
        scale = new BigDecimal(1);
        //离休
        sum = selfFaceAmounts;
        break;
      default:
        break;
    }
  
    return null;
  }
}
